﻿using MediatR;
using Microsoft.Data.SqlClient;
using S.Archives.Domain.S.ArchivesDto.Total;
using S.Archives.Infrstructre;
using S.Archives.Read.Api.Application.Command.Total;

namespace S.Archives.Read.Api.Application.CommandHandlers.Total
{
    /// <summary>
    /// 实时存栏
    /// </summary>
    public class RealTimeColumnCommandHandler : IRequestHandler<RealTimeColumnCommand,List<RealTimeColumnDto> >
    {
        private readonly DbHelper dbHelper;
        public RealTimeColumnCommandHandler(DbHelper dbHelper)
        {
            this.dbHelper = dbHelper;
        }
        public async Task<List<RealTimeColumnDto>> Handle(RealTimeColumnCommand request, CancellationToken cancellationToken)
        {
            var sql = "";
           
            if (request.Id > 0)
            {
                sql = "SELECT  count(DISTINCT EarTag) as Count  FROM SheepFileModel  WHERE PresenceState=2 and FileType=@FileType ";
                
            }
            else
            {
                sql = "SELECT  count(DISTINCT EarTag) as Count  FROM SheepFileModel  WHERE PresenceState=2";
            }
            var parmes = new List<SqlParameter>
            {
                new SqlParameter("@FileType",request.Id)
            };
            var reqult = await dbHelper.GetListDataBySql<RealTimeColumnDto>(sql, parmes.ToArray());

            return reqult;


        }
    }
}
